define(["../common"], function() {
	
	var $form = $("#registerForm");
	var $mobile = $("[name='mobile']");
	var timeIndex = -1;
	var checkUserPath = g_shopDomain + "/member/notExists/json";
	var getCaptchaPath = g_shopDomain + "/member/getCaptcha/json";
	var registerDoPath = g_shopDomain + "/member/registerDo/json";
	// 离下次发送剩余时间
	var remainTime = parseInt($("#remainTime").val()/1000);
	if (remainTime < 0) {
		remainTime = 0;
	}

	// 事件：获取验证码
	$("#getCaptchaBt").click(function() {
		if (!$mobile.isValid() || remainTime > 0) return;

		remainTime = 999; // 随机一个大于0的数，避免重复操作
		$.post(getCaptchaPath, {mobile: $mobile.val()}, function(data) {
			if (data.status !== 0) {
				$form.showMsg(data.message, 'error');
				remainTime = 0;
				setTime();
				return;
			}
			$form.showMsg("验证码已发送，请留意短信信息", 'pass');
			// 可重新发短信倒计时
			remainTime = parseInt(data.result/1000);
			setTime();
		}); 
	});

	$("[id^='submitBt_']").click(function() {
		$form.trigger('validate');
	});

	$form.on('valid.form', function(e, form) {
		if(!$(form).showMsg("正在注册...", 'load', true)) return;
		
		$.post(registerDoPath, $(form).serialize(), function(data) {
			if (data.status !== 0) {
				$(form).showMsg(data.message, 'error');
				return;

			}
			$(form).showMsg("注册成功，即将回到充值页面...", 'pass');
			// 跳转充值页面
			setTimeout(function(){
				location.href = g_shopDomain + "/memberPrepaid/add?name=" + $mobile.val();
			}, 2000);
		});
	});
	
	//可发送短信倒计时
	setTime();
	
	loadjs(["validator"],function() {
		$form.validator({
			fields: {
				mobile: "required;mobile,remote[" + checkUserPath + "]",
				captcha: "required;digits;length[~6]"
			}
		});
	});

	// 倒计时
	function setTime() {
		clearTimeout(timeIndex);
		if (remainTime === 0) {
			$("#getCaptchaBt").removeAttr("style");
			$("#getCaptchaBt").html("获取短信验证码");
			return;
		}
		$("#getCaptchaBt").css({"background-color": "#C1C1C1", "color":"#fff", "border-color": "gray"});
		$("#getCaptchaBt").html(remainTime + "秒后可重发");
		remainTime--;
		timeIndex = setTimeout(function() {
			setTime();
		}, 1000);
	}
});
